#include "Random.h"

#include <ctime>

#include <iostream>

#include "../base.h"

/*
	Random
*/

/*	public	*/

Random& Random::getInstance() {
	if (_instance == 0) {
		/* first call, create the instance */
		_instance = new Random();
	}

	return *_instance;
}

Random::Random() {
	srandom((int)time(0));
}

/*	bool */

bool Random::getBool() {
	return getInt(2) == 0;
}

bool Random::getBool(double probability) {
	return getDouble() < probability;
}

/*	double */

double Random::getDouble() {
	return double(random()) / double(RAND_MAX);
}

double Random::getDouble(double range) {
	return getDouble() * range;
}

double Random::getDoubleCentered(double range) {
	return getDouble(range) - range / 2.0;
}

/*	long	*/

long Random::getLong(long range) {
	return long(double(random()) * double(range) / (double(RAND_MAX) + 1.0));
//	return (long)(((double)(random() + 1) / (double)RAND_MAX) * (double)range);
}

long Random::getLongCentered(long range) {
	return getLong(range) - range / 2;
}

Random* Random::_instance = 0;

